package com.kaiyidiannao.activity.login_or_registered;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import com.kaiyidiannao.activity.user_detail.EditPassword;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.common.util.AppUtils;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.data.Validate;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.req.model.GetIdentifyingCode;
import com.kaiyidiannao.model.req.model.GetValidateCode;
import com.kaiyidiannao.view.ActionBarView;
import com.taplinker.core.util.LogUtil;

import java.util.Observable;
import java.util.Observer;

/**
 * Created by wzg on 14-4-15.
 */
public class ForgetPassword extends BaseActionBarActivity implements Observer {
    private ImageButton sentNum;
    private EditText phoneNum;
    private EditText verification;
    private ImageButton determine;
    private ActionBarView actionBarView;
    String uphone;
    String validateCode;
    String mobile;
    String code = DataCenter.code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_forget_password);
        AppUtils.setupUI(findViewById(R.id.forget), this);
        // 添加观察者
        DataCenter.getInstance().addObserver(this);
        init();
        sentNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (phoneNum.getText().toString() != null && !"".equals(phoneNum.getText().toString())) {
                    if (phoneNum.getText().toString().matches(Validate.phoneValidate)) {
                        uphone = phoneNum.getText().toString();
                        Log.d("aaaaaaa", uphone);
                        showProgressDialog("正在发送");
                        new GetIdentifyingCode(uphone, "1").execute();
                    } else {
                        AlertDialog builder = new AlertDialog.Builder(ForgetPassword.this).setTitle("错误信息")
                                .setMessage("手机号为11位，不能输入空格").setPositiveButton("确定", null).show();
                    }
                }else{
                    AlertDialog builder = new AlertDialog.Builder(ForgetPassword.this).setTitle("错误信息")
                            .setMessage("手机号不能为空").setPositiveButton("确定", null).show();
                }

            }

        });
    }

    public void init() {
        actionBarView = (ActionBarView) findViewById(R.id.forgetActionBar);
        phoneNum = (EditText) findViewById(R.id.uphoneNum);

        actionBarView.title.setText("找回密码");
        sentNum = (ImageButton) findViewById(R.id.sentNum);

        verification = (EditText) findViewById(R.id.verification);
        determine = (ImageButton) findViewById(R.id.determine);
        determine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //判断验证
                if(phoneNum.getText().toString() != null && !"".equals(phoneNum.getText().toString())){
                    if (!"".equals(verification.getText().toString()) && verification.getText().toString() != null) {
                        mobile = phoneNum.getText().toString();
                        validateCode = verification.getText().toString();
                        if (mobile.length() != 11 && validateCode.length() != 6) {
                            AlertDialog builder = new AlertDialog.Builder(ForgetPassword.this).setTitle("错误信息")
                                    .setMessage("手机号为11位，验证码为6位，不能输入空格").setPositiveButton("确定", null).show();
                        } else {
                            showProgressDialog("正在验证");
                            new GetValidateCode("check", validateCode, mobile, "1").execute();
                            Log.d("aaaaaaaaaaa", "------------");

                        }
                    } else {
                        AlertDialog builder = new AlertDialog.Builder(ForgetPassword.this).setTitle("错误信息")
                                .setMessage("验证码不能为空").setPositiveButton("确定", null).show();
                    }
                }else{
                    AlertDialog builder = new AlertDialog.Builder(ForgetPassword.this).setTitle("错误信息")
                            .setMessage("手机号不能为空").setPositiveButton("确定", null).show();
                }

            }
        });
    }

    @Override
    public void update(Observable observable, Object data) {
        DataCenter.flag flag = (DataCenter.flag) data;
closeProgressDialog();
        if (flag == DataCenter.flag.GET_IDENTIFYING_CODE_SUCCESS
                || flag == DataCenter.flag.GET_IDENTIFYING_CODE_FAILED) { //类表数据

            String code = DataCenter.code;
            if (flag == DataCenter.flag.GET_IDENTIFYING_CODE_SUCCESS) {
                if (code.equals(DataCenter.successFlag)) {
                    closeProgressDialog();
                    Toast.makeText(ForgetPassword.this, DataCenter.msg, Toast.LENGTH_SHORT).show();

                } else {
                    closeProgressDialog();
                    Toast.makeText(ForgetPassword.this, DataCenter.msg, Toast.LENGTH_SHORT).show();
                    return;
                }
            }

            if (flag == DataCenter.flag.GET_IDENTIFYING_CODE_FAILED) {
                closeProgressDialog();
            }
        } else if (flag == DataCenter.flag.VALIDATE_IDENTIFYING_CODE_SUCCESS || flag == DataCenter.flag.VALIDATE_IDENTIFYING_CODE_FAILED) {
            String code = DataCenter.code;
            if (flag == DataCenter.flag.VALIDATE_IDENTIFYING_CODE_SUCCESS) {
                if (code.equals(DataCenter.successFlag)) {
                    closeProgressDialog();
                    Toast.makeText(ForgetPassword.this, DataCenter.msg, Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(ForgetPassword.this, EditPassword.class);
                    intent.putExtra("flag", "flag");
                    intent.putExtra("mobile", mobile);
                    startActivity(intent);
                } else {
                    closeProgressDialog();
                    Toast.makeText(ForgetPassword.this, DataCenter.msg, Toast.LENGTH_SHORT).show();
                    return;
                }
            }
            if (flag == DataCenter.flag.VALIDATE_IDENTIFYING_CODE_FAILED) {
                closeProgressDialog();
                //Toast.makeText(ForgetPassword.this, "failed", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.d(DataCenter.LOG_INFO, ForgetPassword.class.getSimpleName() + ".onDestroy");
        //删除观察者
        DataCenter.getInstance().deleteObserver(this);
    }

}
